﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using ClassLibrary.Utilities;

public partial class Admin_Login : System.Web.UI.Page
{
    HttpCookie cookie_username = null;
    HttpCookie cookie_pass = null;
    TblCustomersDAO customerDAO = new TblCustomersDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Cookies["username"] != null || Request.Cookies["pass"] != null )
            {
                txtEmail.Text = Request.Cookies["username"].Value;
                txtpass.Text = Request.Cookies["pass"].Value;

            }
        }
    }
    protected void btLogin_Click(object sender, EventArgs e)
    {
        bool bFlag = false;
        try
        {
            // khai báo biến cookie
            cookie_username = new HttpCookie("username", txtEmail.Text);
            cookie_pass = new HttpCookie("pass", txtpass.Text);

            // Gan ngay song cho cookie
            cookie_username.Expires = DateTime.Now.AddDays(30);
            cookie_pass.Expires = DateTime.Now.AddDays(30);

            //Them cookie

            if (cbRemember.Checked)
            {
                cookie_username.Value = txtEmail.Text;
                cookie_pass.Value = txtpass.Text;
                Response.Cookies.Add(cookie_username);
                Response.Cookies.Add(cookie_pass);
            }
            else
            {
                cookie_username.Value = null;
                cookie_pass.Value = null;
            }
            if (txtEmail.Text != "" && txtpass.Text != "")
            {

                TblCustomers userAccount = customerDAO.GetAdmin(txtEmail.Text.Trim(), Encryption.EncryptToString(txtpass.Text));
                if (userAccount != null)
                {


                    Session[Global.SESSION_ADMIN_USER] = userAccount;
                    bFlag = true;
                }
                else
                {
                    lbMessenge.Text = "Tên đăng nhập ( mật khẩu ) chưa trùng khớp .";
                    lbMessenge.Visible = true;
                }

            }
            else
            {
                lbMessenge.Text = "Tên đăng nhập và mật khẩu không được rổng";
                lbMessenge.Visible = true;
            }
            
         }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (bFlag)
            Response.Redirect("~/Admin/Default.aspx");
    }
}
